package com.publicize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.publicize.commom.commonutils.Result;
import com.publicize.entity.Examination;
import com.publicize.entity.ExaminationDetail;
import com.publicize.entity.User;
import com.publicize.entity.UserCompetition;
import com.publicize.entity.Vo.ScoreVo;
import com.publicize.entity.Vo.twoOption;
import com.publicize.entity.Vo.threeOption;
import com.publicize.entity.Vo.oneOption;
import com.publicize.mapper.ExaminationDetailMapper;
import com.publicize.service.ExaminationDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.publicize.service.ExaminationService;
import com.publicize.service.UserCompetitionService;
import com.publicize.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 知识竞赛题目表 服务实现类
 * </p>
 *
 * @author bs
 * @since 2021-03-06
 */
@Service
public class ExaminationDetailServiceImpl extends ServiceImpl<ExaminationDetailMapper, ExaminationDetail> implements ExaminationDetailService {
    @Autowired
    private ExaminationDetailService examinationDetailService;
    @Resource
    private ExaminationService examinationService;
    @Resource
    private UserCompetitionService userCompetitionService;
    @Resource
    private UserService userService;
//封装试题接口

//统计分数
@Override
public List<ExaminationDetail> sentExamination(String examinationNumber) {
    QueryWrapper<ExaminationDetail> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("examination_number",examinationNumber);
    List<ExaminationDetail> result=examinationDetailService.list(queryWrapper);



    return result;
}
    //统计分数
    @Override
    public Result statisticalScore(ScoreVo scoreVo, String userId, Result result) {
        //根据编号获取该试题
        Examination examination = examinationService.getById(scoreVo.getNumber());
        //根据试题的总数题目数算出每道题分数
        Integer topicScore = examination.getTopicScore();
        Integer topicNum = examination.getTopicNum();
        Integer score=topicScore/topicNum;
        //记录总分
        Integer resultScore=0;
        //获取用户填入的分数
        Map<String,String> userMap=scoreVo.getUserAnswer();
        List<Integer> ret=new ArrayList<>();
        //根据试题编号查出试题的所有答案
        QueryWrapper<ExaminationDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("examination_number",scoreVo.getNumber());
        //获取所有有关试题
        List<ExaminationDetail> examinationDetails = baseMapper.selectList(queryWrapper);
        //根据每个试题编号,在map中寻找相对的答案检验
        for (ExaminationDetail examinationDetail:examinationDetails){

            String answer=userMap.get(examinationDetail.getId());
            if(answer!=null){
                if(answer.equals(examinationDetail.getAnswer())) {
                    ret.add(1);
                    resultScore+=score;
                }else{
                    ret.add(0);
                }

            }
        }
        //插入用户总分
        UserCompetition userCompetition=new UserCompetition();
        userCompetition.setUserId(userId);
        userCompetition.setNumber(scoreVo.getNumber());
        userCompetition.setScore(resultScore);
        userCompetitionService.save(userCompetition);
        //更新用户平均分
        //1 查询用户每次比赛的分数
        QueryWrapper<UserCompetition> wrapper=new QueryWrapper<>();
        //根据用户id查询
        wrapper.eq("user_id",userId);
        List<UserCompetition> Scorelist= userCompetitionService.list(wrapper);
        Integer Score=0;
        //2 获取总分
        for(UserCompetition userCo:Scorelist){
            Score+=userCo.getScore();
        }
        //3 算出平均分
        Score=Score/Scorelist.size();
        //更新用户表平均分
        User user=new User();
        user.setId(userId);
        user.setUserAverageScore(Score);
        userService.updateById(user);
        //返回结果


        return result.data("score",resultScore).data("resultList",ret).data("userMap",userMap);
    }
//展示前十排名
    @Override
    public List<User> showRank() {
        List<User> list=new ArrayList<>();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("user_average_score");
        List<User> users = userService.list(queryWrapper);
        for (int i = 0; i <10 ; i++) {
            list.add(users.get(i));
        }
        return list;
    }
}
